package com.googlecode.flightreports.citydetails;

import java.util.Iterator;
import java.io.IOException;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import com.googlecode.kohmori.reportgen.AbstractXMLBuilder;

public class ReportXMLBuilder extends AbstractXMLBuilder {
  
    public void parse(InputSource input) throws IOException, SAXException {
        handler.startDocument();
        generateFor((ReportObject)input);
        handler.endDocument();        
    }
    
    protected void generateFor(ReportObject report) throws SAXException {       
        handler.startElement("cityDetailsReport");

        handler.startElement("queryParams");
        handler.element("cityName", report.getQueryParams().getCityName());
        handler.endElement("queryParams");

        handler.element("runDate", report.getRunDate(), runDateFormat);
        handler.element("country", report.getCountry());
        handler.element("airport", report.getAirport());
        handler.element("language", report.getLanguage());
        handler.element("countryISOCode", report.getCountryISOCode());

        handler.startElement("arrivals");
        Iterator i = report.getArrivalItems().iterator();
        while (i.hasNext()) {
        	ReportObject.ArrivalItem item 
                    = (ReportObject.ArrivalItem) i.next();
            generateFor(item);
        }
        handler.endElement("arrivals");

        handler.startElement("departures");
        i = report.getDepartureItems().iterator();
        while (i.hasNext()) {
        	ReportObject.DepartureItem item 
                    = (ReportObject.DepartureItem) i.next();
            generateFor(item);
        }
        handler.endElement("departures");

        handler.endElement("cityDetailsReport");
    }

    protected void generateFor(ReportObject.ArrivalItem item) throws SAXException {
        handler.startElement("arrival");
        handler.element("flightNumber", item.getFlightNumber());
        handler.element("origin", item.getOrigin());
        handler.element("departTime", item.getDepartTime());
        handler.element("arriveTime", item.getArriveTime());
        handler.element("miles", item.getMiles());
        handler.element("numberOfSegments", item.getNumberOfSegments());
        handler.element("aircraftType", item.getAircraftType());
        handler.endElement("arrival");
    }
    
    protected void generateFor(ReportObject.DepartureItem item) throws SAXException {
        handler.startElement("departure");
        handler.element("flightNumber", item.getFlightNumber());
        handler.element("destination", item.getDestination());
        handler.element("departTime", item.getDepartTime());
        handler.element("arriveTime", item.getArriveTime());
        handler.element("miles", item.getMiles());
        handler.element("numberOfSegments", item.getNumberOfSegments());
        handler.element("aircraftType", item.getAircraftType());
        handler.endElement("departure");
    }    
}
